

use "I:\Workdata\702525\Alex\persondata_v2.dta", clear

drop if cvrnr==.
drop if lonind==.
drop if lonind<=1
merge m:1 year using "D:\Workdata\702525\users\Daniel\DecentralizationSorting\Alex\deflator", nogen keep(1 3)
replace lonind=lonind*100/deflator
replace timelon=timelo*100/deflator

ren arledgr frac_unemployed
ren tlonkval quality_hour
ren aldernov age
gen max_educ=int(hffsp/1000000)
gen educ=1 if max_educ<=25
replace educ=2 if max_educ==35
replace educ=3 if max_educ>=40&max_educ<=50
replace educ=4 if max_educ>=60&max_educ<.
drop if educ==.
drop if frac_unemployed>500
drop if exper==.

replace timelon=lonind/1800 if timelon==0
gen y=log(timelon)
format pnr %12.0f
egen firm=group(cvrnr year)
ren pnr id
recast long firm
replace exper=int(exp) 
merge m:1 cvrnr year using "$home\firmdata_v1", keep(1 3) keepus(vapp) nogen
merge m:1 cvrnr year using "$home\fire\fire", keep(1 3) keepus(oms aarsv) nogen
gen salepp=log(oms/aarsv)
replace vapp=6.1 if vapp==.
replace salepp=7.2 if salepp==.
replace vapp=round(vapp, 0.001)
replace salepp=round(salepp, 0.001)
replace vapp=0.001 if vapp<=0
replace salepp=0.001 if salepp<=0
replace vapp=vapp*1000
replace salepp=salepp*1000

bysort cvrnr year: gen emp=_N
bysort cvrnr: egen maxemp=max(emp)
drop if maxemp<=5

preserve
collapse firm, by(cvrnr year)
compress
save "$project/manager/firmyearid_cvrnr_over5", replace
restore

preserve
tsset id year
gen hire=(cvrnr!=L.cvrnr)
gen fire=(F.cvrnr!=cvrnr)
gen nworker=1
collapse (sum) hire fire nworker, by (cvrnr year)
tsset cvrnr year
gen hiring_rate=hire/L.nworker
gen firing_rate=fire/nworker
compress
save "$project/hiring_firing_rate", replace
restore

keep     id year firm y age educ exper vapp salepp
order    id year firm y age educ exper vapp salepp
compress    year      y age educ exper vapp salepp
sort id year

outfile using "$project\manager\personyear_panel_cvrnr_over5", wide comma replace

*run lbnryearfe.m here

import delimited pnr firm year pe fe xb using "$project\manager\cvrnryear_fe_over5.txt", clear
recast double pnr firm
merge m:1 firm using "$project/manager/firmyearid_cvrnr_over5", keep(1 3) nogen
gen emp=1
collapse  (mean) fe pe (sum) emp, by (cvrnr year)
ren pe avgpe
merge 1:1 cvrnr year using "$project\manager\manager_all_cvrnr2", keep(1 3) keepus(pnr) nogen
drop if pnr==.

save "$project\manager\cvrnryear_fe_over5", replace

use "$project\manager\cvrnryear_fe_over5", clear

format pnr %12.0f
ren pnr id
ren fe y
egen firm=group(cvrnr)

preserve
collapse firm, by(cvrnr)
compress
save "$project/manager/firmid_over5", replace
restore

gen year_old=year
gen cvrnr_old=firm
gen firmyearid=_n

keep     cvrnr_old year_old firm year id firmyearid y emp
order    cvrnr_old year_old firm year id firmyearid y emp
compress    year      y 
sort id year

outfile using "$project\manager\firmyear_panel_cvrnr_over5", wide comma replace

*run managerfe.m here

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_over5.txt", clear
recast double pnr firm
merge m:1 firm using "$project/manager/firmid_over5", keep(1 3) nogen

save "$project\managerfe_firm_over5"


/******************************************
******ONE STEP***************************
******************************************/

insheet using "$project\manager\personyear_panel_cvrnr_over5", clear
ren v1 pnr
ren v2 year
ren v3 firm
ren v4 y
ren v5 age
ren v6 educ
ren v7 exper
drop v8 v9
recast double pnr firm
merge m:1 firm using "$project/manager/firmyearid_cvrnr_over5", keep(1 3) nogen
ren pnr pnr0
merge m:1 cvrnr year using "$project\managerfe_firm_over5", keepus(pnr) keep(3) nogen
ren pnr pnrm
ren pnr0 pnr
drop firm
egen firm=group(cvrnr)
egen manager=group(pnrm)

preserve
collapse firm, by(cvrnr)
compress
save "$project/manager/firmid_1step_over5", replace
restore

preserve
collapse manager, by(pnrm)
compress
save "$project/manager/managerid_1step_over5", replace
restore

keep      pnr year firm manager y age educ exper
order     pnr year firm manager y age educ exper
sort pnr year
format pnr %12.0f

outfile using "$project\manager\personyear_panel_1step_over5", wide comma replace

*run managerfe_1step.m here

import delimited pnr firm manager year pe fe me mevar xb using "$project\manager\manager_fe_1step_over5.txt", clear
recast double pnr firm manager
merge m:1 firm using "$project/manager/firmid_1step_over5", keep(1 3) nogen
merge m:1 manager using "$project/manager/managerid_1step_over5", keep(1 3) nogen

save "$project\akm_1step_over5", replace

collapse me mevar, by (pnrm)

capture noisily ebayes me mevar, gen(me_bayes) var(mevar_bayes) uvar(meuvar_bayes)

save "$project\managerfe_1step_over5", replace


****************************************
*****Other outcomes*********************
****************************************

use "$project\manager\cvrnryear_fe_over5", clear

format pnr %12.0f
ren pnr id
ren fe y
egen firm=group(cvrnr)

merge m:1 cvrnr year using "$home\firmdata_v1", keep(1 3) keepus(vapp tfp_op) nogen
merge m:1 cvrnr year using "$home\fire\fire", keep(1 3) keepus(oms arf at egul krh krhe kvv) nogen
merge m:1 cvrnr year using "$project\merger_cvrnr", keep(1 3) keepus(nworker) nogen
merge m:1 cvrnr year using "$project/hiring_firing_rate", keep(1 3) keepus(hiring_rate firing_rate) nogen

gen roa=arf/at
gen leverage=1-egul/at
gen logemp=log(nworker)
gen input=krh/oms
replace input=(krhe+kvv)/oms if input==.

gen year_old=year
gen cvrnr_old=firm
gen firmyearid=_n

foreach var in vapp tfp_op leverage input logemp roa hiring_rate {
	sum `var',d
	replace `var'=r(p1) if `var'<r(p1)
	replace `var'=r(p99) if `var'>r(p99) & `var'!=.
}

keep     cvrnr_old year_old firm year id firmyearid y emp vapp tfp_op leverage input logemp roa hiring_rate firing_rate
order    cvrnr_old year_old firm year id firmyearid y emp vapp tfp_op leverage input logemp roa hiring_rate firing_rate
compress    year      y 
sort id year

outfile using "$project\manager\firmyear_panel_cvrnr_other", wide comma replace

*run managerfe_other.m here

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_tfp.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_tfp) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_tfp", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_vapp.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_vapp) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_vapp", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_leverage.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_leverage) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_leverage", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_input.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_input) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_input", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_logemp.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_logemp) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_logemp", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_roa.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_roa) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_roa", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_hiring_rate.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_hiring_rate) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_hiring_rate", replace

import delimited firm year pnr pe pevar fe xb r y using "$project\manager\manager_fe_firing_rate.txt", clear
recast double pnr firm
collapse pe pevar, by (pnr)
capture noisily ebayes pe pevar, gen(pe_firing_rate) var(mevar_bayes) uvar(meuvar_bayes)
save "$project\manager\manager_fe_firing_rate", replace



/****************************************
*****Robustness: add ind year and region year FE*********************
****************************************


insheet using "$project\manager\personyear_panel_cvrnr_over5", clear
ren v1 pnr
ren v2 year
ren v3 firm
ren v4 y
ren v5 age
ren v6 educ
ren v7 exper
drop v8 v9
recast double pnr firm
merge m:1 firm using "$project/manager/firmyearid_cvrnr_over5", keep(1 3) nogen
ren pnr pnr0
merge m:1 cvrnr year using "$project\managerfe_firm_over5", keepus(pnr) keep(3) nogen
ren pnr pnrm
ren pnr0 pnr
drop firm
egen firm=group(cvrnr)
egen manager=group(pnrm)
merge m:1 cvrnr year using "$project\temp_ind_region", keep(1 3) nogen
replace region=1 if region==.
egen regionyear=group(region year)
replace ind36num=0 if ind36num==6 | ind36num==23 | ind36num==37 | ind36num==33 | ind36num==34
egen indyear=group(ind36num year)

preserve
collapse manager, by(pnrm)
compress
save "$project/manager/managerid_1step_addfe", replace
restore

keep      pnr year firm manager y age educ exper regionyear indyear
order     pnr year firm manager y age educ exper regionyear indyear
sort pnr year
format pnr %12.0f

outfile using "$project\manager\personyear_panel_1step_addfe", wide comma replace

*run managerfe_1step_addfe.m here

import delimited pnr firm manager year pe fe me mevar using "$project\manager\manager_fe_1step_addfe.txt", clear
recast double pnr firm manager
merge m:1 manager using "$project/manager/managerid_1step_addfe", keep(1 3) nogen

collapse me mevar, by (pnrm)

capture noisily ebayes me mevar, gen(me_bayes) var(mevar_bayes) uvar(meuvar_bayes)

save "$project\managerfe_1step_addfe", replace
